/*
* @Author: fangshuqin
* @Date:   2018-05-07 14:24:22
* @Last Modified by:   ZHELIHAO\fangshuqin
* @Last Modified time: 2018-05-26 19:12:52
*/
{
    let EVENT_CLICK = Laya.Event.CLICK;

    
    class Rolelist extends ui.Role.ListUI {
        constructor () {
            super();
            
            this.init();

        }

        init () {
            
            this.initConfig();
            this.initEvent();
            this.registerActions();
            // Sail.io.emit(GAME_CMDS.GETHEROLIST);
        }

        // 初始化配置
        initConfig() {
            this.config = {
                MAX_WIDTH: 100,
                MIN_WIDTH: 12

            }
            this.heroListAll = [];
            this.heroListLong = [];
            this.heroListNear = [];
            this.heroListCurrent = [];
            this.lastCheck = 0;
            this.kindIndex = 0;

            this.isFirst = true;
        }

   
         registerActions() {

            this.ACTIONS = {
                 [GAME_CMDS.GETHEROLIST]  : this.getHeroList,    //列表
                 [GAME_CMDS.UPBYITEM]     : this.upByItemHero,   //升级

            };
            Sail.io.register(this.ACTIONS, this);
        }

        initEvent(){

            this.list_role_all.selectEnable = true;
            this.list_role_all.selectHandler = Laya.Handler.create(this, this.singleRole, null, false);

            this.tab_nav.selectHandler = Laya.Handler.create(this, this.getHeroTypeList, null, false);

            this.btn_arrow.on(EVENT_CLICK, this, ()=>{
                if(this.kindBox.visible){
                    this.kindBox.visible = false;
                }else{
                    this.kindBox.visible = true;
                }

            })

            // 点击其他区域分类菜单隐藏
            Laya.stage.on(EVENT_CLICK, this, (event)=>{

                if(!event) return;
                let _target = event.target;
                let _targetBox = this.kindBox;

                if(_targetBox && _targetBox.visible && _target!== this.btn_arrow && !_targetBox.contains(_target)){
                    _targetBox.visible = false;
                }
            })
        }

        getHeroList(data){
            console.log("getHeroList", data);
            let result =[];
            let _length = data.length;
            let _odd = Math.floor( (_length+1)/2 );
            this.list_role_all.height = _length< 7 ? 355 * _odd :1060;
            this.list_role_all.vScrollBarSkin = "";

            data.forEach((item,index)=>{
                let _heroList = item;
                let _grade = Math.floor( _heroList.grade /5 );


                let _shadow = _heroList.had;

                // 英雄基本信息
                let heroNo = Number(_heroList.hero_no);
                let localHero = Sail.store.getHeroData(heroNo);

                // 英雄段位数据
                let heroGrade = Number(_heroList.grade);
                let localHeroGrade = Sail.store.getHeroGrade(heroGrade);

                // 段位
                let grade_mole = _heroList.cup_num - localHeroGrade.cup_num;  //分子
                    grade_mole > 0 ? grade_mole : 0 ;
                let grade_deno = localHeroGrade.next_cup_num;          //分母
                let grade_progress = grade_mole / grade_deno;

                let grade_per = Math.floor( grade_progress * 100 );
                let grade_w = grade_per / 100 * this.config.MAX_WIDTH;

                grade_w = grade_w < this.config.MIN_WIDTH ? this.config.MIN_WIDTH : grade_w; 

                result.push({
                    quality: {
                        skin:`res/public/frame_${localHero.quality}.png`
                        },
                    quality2: {
                        skin:`res/public/rolebg_${localHero.quality}.png`
                        },
                    role:{
                        skin:`res/public/role_${_heroList.hero_no}.png`
                        },
                    level: _heroList.level + '级',
                    progress:{
                        width:grade_w
                    },
                    cupnum: _heroList.cup_num,
                    grade:{
                        index : _grade
                    },
                    gradenum:_heroList.grade,
                    shadow : {
                        visible: _shadow == 0 ? true :false
                    }
                })

                // 遮罩：多余box隐藏
                    let hideBox  =  this.list_role_all.getCell(index);
                    let bg_pro = hideBox.getChildByName("bg_pro");
                    let bg_cup = hideBox.getChildByName("bg_cup");
                    let progress = hideBox.getChildByName("progress");
                    let cupnum = hideBox.getChildByName("cupnum");
                    let grade = hideBox.getChildByName("grade");
                    let gradenum = hideBox.getChildByName("gradenum");
                    let bg_lvl = hideBox.getChildByName("bg_lvl");
                    let level = hideBox.getChildByName("level");
                if(_shadow ==0){
                    bg_pro.visible = false;
                    bg_cup.visible = false;
                    progress.visible = false;
                    cupnum.visible =false;
                    grade.visible =false;
                    gradenum.visible = false;
                    bg_lvl.visible = false;
                    level.visible = false;
                    
                }else{
                    bg_pro.visible = true;
                    bg_cup.visible = true;
                    progress.visible = true;
                    cupnum.visible =true;
                    grade.visible =true;
                    gradenum.visible = true;
                    bg_lvl.visible = true;
                    level.visible = true;
                }

                if( this.isFirst ){
                    if(_heroList.type == 0){
                        this.heroListLong.push(_heroList);
                    }else if(_heroList.type == 1){
                        this.heroListNear.push(_heroList);
                    }
                    this.heroListAll.push(_heroList);
                    this.heroListCurrent.push(_heroList);
                }
                
                
            })

            this.list_role_all.array = result;
            this.list_role_all.getCell(0).getChildByName("check").visible = true;
            this.list_role_all.selectedIndex = 0;
            this.lastCheck = 0;
            this.isFirst   = false;
        }

        // 英雄类型：全部 远程 近战
        getHeroTypeList(index){

            this.kindIndex = index;
            this.list_title.index = index;
            this.kindBox.visible = false;

            let dataList = null;
            if(index === 0){
                dataList = this.heroListCurrent = this.heroListAll;
            }else if(index === 1){
                dataList = this.heroListCurrent = this.heroListLong;
            }else if(index === 2){
                dataList = this.heroListCurrent = this.heroListNear;
            }

            let lastCell = this.list_role_all.getCell(this.lastCheck);
            let lastCheck = lastCell.getChildByName("check");
            lastCheck.visible = false;

            this.getHeroList(dataList);
            this.singleRole(0, 'kind');

        }

        // 单个角色切换
        singleRole(index, kind){
            let heroNo =  Number(this.heroListCurrent[index].hero_no);
            let level  = this.heroListCurrent[index].level;
            let chips = this.heroListCurrent[index].chips;

             // 英雄基本信息
            let localHero = Sail.store.getHeroData(heroNo);
            localHero.grade  = Number(this.heroListCurrent[index].grade);
            localHero.cup_num = Number(this.heroListCurrent[index].cup_num);

            kind === 'kind' ? true : false;
            if(!kind){
                let currentCell = this.list_role_all.getCell(index);
                let cerrentCheck = currentCell.getChildByName("check");

                if( this.lastCheck != -1){
                    let lastCell = this.list_role_all.getCell(this.lastCheck);
                    let lastCheck = lastCell.getChildByName("check");
                    lastCheck.visible = false;
                }
                cerrentCheck.visible = true;

                this.lastCheck = index; 
            }

            
            // 英雄id
            let localHeroLevel = Sail.store.getHeroLevel(heroNo);

            localHeroLevel.curLevel = level;
            localHeroLevel.chips = chips;

            Sail.io.publish("getUserOneHeroInfo", localHero);


            Sail.io.publish("heroLevel", localHeroLevel);
            

            // Sail.io.emit(GAME_CMDS.GETUSERONEHEROINFO, {heroNo: heroNo,level:level});
        }

        // 升级后更新
        upByItemHero(data, code){
            if( code !==0 ) return;
            console.log("upByItemHero",data);
            let alreadyCheckHero = this.list_role_all.getCell(this.lastCheck);
            let dom_quality = alreadyCheckHero.getChildByName("quality");
            let dom_quality2 = alreadyCheckHero.getChildByName("quality2");
            let dom_role = alreadyCheckHero.getChildByName("role");

            let dom_progress = alreadyCheckHero.getChildByName("progress");
            let dom_cupnum = alreadyCheckHero.getChildByName("cupnum");
            let dom_level = alreadyCheckHero.getChildByName("level");
            let dom_grade = alreadyCheckHero.getChildByName("grade");
            let dom_gradenum = alreadyCheckHero.getChildByName("gradenum");

            let _upprogress = data.cup_num/ data.next_cup_num ;

               let upper = Math.floor( _upprogress * 100 );
               let upw = upper / 100 * this.config.MAX_WIDTH;
               upw = upw < this.config.MIN_WIDTH ? this.config.MIN_WIDTH : upw;

            let _grade = Math.floor( data.grade /5);    

            dom_quality.skin = `res/public/frame_${data.quality}.png`;
            dom_quality2.skin =  `res/public/rolebg_${data.quality}.png`;
            dom_role.skin = `res/public/role_${data.hero_no}.png`;
            dom_level.text = data.level;
            dom_progress.width = upw;
            dom_cupnum.text = data.cup_num;
            dom_grade.index = _grade;
            dom_gradenum.text = data.grade;

        }

        exit () {
            Sail.io.unregister(this.ACTIONS);
        }

        resize (width, height, rate) {
            if(rate < 1){
                this.scale(rate, rate);
            }else{
                this.scale(1, 1);
            }
        }

    }

    Sail.class(Rolelist, "Com.Role.Rolelist");
}